package com.example.campustalent.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.campustalent.R
import com.example.campustalent.bean.ContentBean
import com.example.campustalent.bean.FavorBean
import com.example.campustalent.bean.StudyInfo
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.ActivityDetailBinding
import com.example.campustalent.databinding.ActivityMainBinding
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.SharedPreferencesUtils

class DetailActivity : AppCompatActivity() {
    private var mTitle: String? = null
    private lateinit var preferencesUtils: SharedPreferencesUtils
    private lateinit var binding: ActivityDetailBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDetailBinding.inflate(layoutInflater)
        val rootView: View = binding.root
        setContentView(rootView)
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(this, "CampusTalent")
        }
        preferencesUtils = SharedPreferencesUtils(this)
        mTitle = intent?.getStringExtra("title")
//        val mUrl: String? = intent?.getStringExtra("imageUrl")
//        Log.e("图片", mUrl!!)
        intent?.getStringExtra("imageUrl")?.let {
            Glide.with(this).load(R.mipmap.img).into(binding.ivImage)
            binding.ivImage.visibility = View.VISIBLE
        }
        val list = DbUtils.getQueryByWhere(FavorBean::class.java, "name", arrayOf(preferencesUtils.getParam("name", "").toString()), "title", arrayOf(mTitle!!))
        if (list.isNullOrEmpty()){
            binding.ivGive.setImageResource(R.mipmap.ungive)
            binding.ivFavor.setImageResource(R.mipmap.unfavor)
            val favor = FavorBean()
            favor.collection = false
            favor.favor = false
            favor.title = mTitle
            favor.name = preferencesUtils.getParam("name", "").toString()
            DbUtils.insert(favor)
        }else {
            if (list[0].favor){
                binding.ivFavor.setImageResource(R.mipmap.favor)
            }else {
                binding.ivFavor.setImageResource(R.mipmap.unfavor)
            }
            if (list[0].collection){
                binding.ivGive.setImageResource(R.mipmap.give)
            }else {
                binding.ivGive.setImageResource(R.mipmap.ungive)
            }

        }
        binding.tvTitle.text = mTitle
        binding.tvContent.text = intent?.getStringExtra("content")
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.ivFavor.setOnClickListener {
            val listNew = DbUtils.getQueryByWhere(FavorBean::class.java, "name", arrayOf(preferencesUtils.getParam("name", "").toString()), "title", arrayOf(mTitle!!))
            if (listNew.isNullOrEmpty()){
                val favor = FavorBean()
                favor.collection = false
                favor.favor = true
                favor.title = mTitle
                favor.name = preferencesUtils.getParam("name", "").toString()
                DbUtils.insert(favor)
                binding.ivFavor.setImageResource(R.mipmap.favor)
            }else {
                if (listNew[0].favor){
                    listNew[0].favor = false
                    binding.ivFavor.setImageResource(R.mipmap.unfavor)
                }else {
                    listNew[0].favor = true
                    binding.ivFavor.setImageResource(R.mipmap.favor)
                }
                DbUtils.update(listNew[0])
            }
        }
        binding.ivGive.setOnClickListener {
            val listNew = DbUtils.getQueryByWhere(FavorBean::class.java, "name", arrayOf(preferencesUtils.getParam("name", "").toString()), "title", arrayOf(mTitle!!))
            if (listNew.isNullOrEmpty()){
                val favor = FavorBean()
                favor.collection = true
                favor.favor = false
                favor.title = mTitle
                favor.name = preferencesUtils.getParam("name", "").toString()
                DbUtils.insert(favor)
                binding.ivGive.setImageResource(R.mipmap.give)
            }else {
                if (listNew[0].collection){
                    listNew[0].collection = false
                    binding.ivGive.setImageResource(R.mipmap.ungive)
                }else {
                    listNew[0].collection = true
                    binding.ivGive.setImageResource(R.mipmap.give)
                }
                DbUtils.update(listNew[0])
            }
        }

    }
    private fun setData(isFavor: Boolean, collection: Boolean){

    }
}